<template>
  <el-card class="mt-10">
    <!-- 
      按钮禁用效果
        三级分类id有值，不需要禁用 false
        三级分类id没值，需要禁用 true
     -->
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showAddAttr"
    >
      添加属性
    </el-button>

    <el-table :data="attrList" border class="mt-20">
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column label="属性名称" prop="attrName" width="150" />
      <el-table-column label="属性值列表">
        <template v-slot="{ row }">
          <el-tag
            v-for="attrValue in row.attrValueList"
            :key="attrValue.id"
            class="attr-tag"
          >
            {{ attrValue.valueName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template v-slot="{ row }">
          <el-tooltip effect="light" content="修改属性" placement="bottom">
            <el-button
              type="warning"
              :icon="Edit"
              size="small"
              @click="showUpdateAttr(row)"
            />
          </el-tooltip>

          <el-tooltip effect="light" content="删除属性" placement="bottom">
            <span class="attr-btn">
              <el-popconfirm
                :title="`您确定要删除 ${row.attrName} 吗`"
                @confirm="delAttr(row.id)"
              >
                <template #reference>
                  <el-button type="danger" :icon="Delete" size="small" />
                </template>
              </el-popconfirm>
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AttrList",
};
</script>

<script lang="ts" setup>
import { watch, ref } from "vue";
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { getAttrListApi, delAttrApi } from "@/api/product/attr";
import type { AttrList, AttrItem } from "@/api/product/model/attrModel";
import { useCategoryStore } from "@/stores/category";
import { useAttrStore } from "@/stores/attr";
import { ElMessage } from "element-plus";

const emit = defineEmits(["setIsShowAttrList"]);

// 进入添加属性
const showAddAttr = () => {
  // 重置数据为初始化值
  attrStore.$reset();
  emit("setIsShowAttrList", false);
};

const attrStore = useAttrStore();
// 进入修改属性
const showUpdateAttr = (row: AttrItem) => {
  attrStore.$patch({
    id: row.id,
    attrName: row.attrName,
    attrValueList: row.attrValueList,
  });
  emit("setIsShowAttrList", false);
};

const attrList = ref<AttrList>([]);
// 当用户同时选中一级、二级、三级时，需要发送请求，请求属性列表
const categoryStore = useCategoryStore();

const getAttrList = async () => {
  const { category1Id, category2Id, category3Id } = categoryStore;

  attrList.value = await getAttrListApi({
    category1Id: category1Id as number,
    category2Id: category2Id as number,
    category3Id: category3Id as number,
  });
};

watch(
  () => categoryStore.category3Id,
  async (category3Id) => {
    // 请求属性列表

    if (!category3Id) {
      attrList.value = [];
      return;
    }

    getAttrList();
  },
  { immediate: true }
);

// 删除属性
const delAttr = async (id: number) => {
  await delAttrApi(id);
  ElMessage.success("删除属性成功");
  getAttrList();
};
</script>

<style scoped>
.attr-tag {
  margin-right: 10px;
}

.attr-btn {
  display: inline-block;
  margin-left: 10px;
}
</style>
